/*
 * FileName: FileController.java
 *
 */
package com.mrc.core.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author Mrc
 * @version 4.1

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2017-1-7 下午5:15:23          Mrc        4.1         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Controller
@RequestMapping(value = "/fileController.do")
public class FileController {

    //    private Logger logger = Logger.getLogger(FileController.class);

    /**
     * 下载图片接口
     * @param filePath 文件在服务器上的路径
     * @param fileName 文件名
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(params = "method=downImage")
    public void down(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //文件在服务器上的路径
        String filePath = request.getParameter("filePath");
        File file = new File(filePath);// path是根据日志路径和文件名拼接出来的
        String filename = file.getName();// 获取日志文件名称
        try {
            InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
            // 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"), "iso8859-1"));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream os = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            os.write(buffer);// 输出文件
            os.flush();
            os.close();
        } catch (Exception e) {
            response.sendError(404, "文件不存在");
        }
    }

    //    /**
    //     * 根据路径获得文件转换成流返回
    //     * @param filePath
    //     * @return
    //     */
    //    private InputStream getInputStream(String filePath) {
    //        logger.debug("转文文件: " + filePath);
    //        File file = new File(filePath);
    //        if (!file.exists()) {
    //            logger.debug("文件不错在!!!!!!!!!!!!!!!!");
    //            return null;
    //        }
    //        InputStream is = null;
    //        try {
    //            is = new FileInputStream(file);
    //        } catch (FileNotFoundException e) {
    //            e.printStackTrace();
    //        } finally {
    //            try {
    //                is.close();
    //            } catch (IOException e) {
    //                e.printStackTrace();
    //            }
    //        }
    //        return is;
    //    }

}
